import { getLocal, setLocal, removeLocal } from '@/utils/utils';
import router from '@/router';
import { userLogin } from '@/api/user';

export default {
  state: {
    user: getLocal('user') || {},
  },
  mutations: {
    saveUser(state, { user }) {
      state.user = user;
    },
  },
  actions: {
    async userLogin({ commit }, { username, password }) {
      try {
        commit('showLoading');
        const result = await userLogin(
          {
            username,
            password,
          },
        );
        commit('saveUser', { user: result.user });
        setLocal('user', result.user);
        router.push('/restaurant');
      } catch (error) {
        commit('showErr', { message: error.message });
        // console.log(error.message);
      } finally {
        commit('hideLoading');
      }
    },
    logout({ commit }) {
      removeLocal('user');
      commit('saveUser', { user: {} });
      router.push('/login');
    },

  },
  modules: {
  },
  getters: {
  },
};

// state: {
// },
// mutations: {
// },
// actions: {
// },

// modules: {
// },
// getters: {
// },
